package com.ias.agdis_r.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.util.network.WifiUtil;

public class WifiAdapter extends BaseAdapter{
	
	private List<HashMap<String,Object>> wifiList= null;
	private LayoutInflater inflater = null;  
	private WifiUtil wifiUtil = null;
	private Context context;
	public WifiAdapter(Context context){
		this.context = context;
		wifiUtil = new WifiUtil(context);
		wifiList = wifiUtil.getWifiList();
	}
	
	public void updateUI(){
		
		wifiList = wifiUtil.getWifiList();
		this.notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return wifiList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		ViewHolder holder = new ViewHolder();
		if(null == convertView){
			inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.item_wifi, null);
			holder.txt_ssid = (TextView)convertView.findViewById(R.id.txt_ssid);
			holder.txt_state = (TextView)convertView.findViewById(R.id.txt_state);
			holder.img_state = (ImageView)convertView.findViewById(R.id.img_state);
			holder.img_rssi = (ImageView)convertView.findViewById(R.id.img_rssi);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		HashMap<String,Object> wifiMap = wifiList.get(position);
		//String bssid = (String) wifiMap.get(WifiUtil.TAG_BSSID);
		String ssid = (String) wifiMap.get(WifiUtil.TAG_SSID);
		String type = (String) wifiMap.get(WifiUtil.TAG_TYPE);
		boolean state = (boolean) wifiMap.get(WifiUtil.TAG_STATE);
		int level = (int) wifiMap.get(WifiUtil.TAG_LEVEL);
		holder.txt_ssid.setText(ssid);
		holder.img_rssi.setBackgroundResource(getWifiLevelImageId(level));
		holder.txt_state.setText(getStateText(state,type));
		if(state)
		{
			holder.img_state.setVisibility(View.VISIBLE);
			holder.txt_ssid.setTextColor(context.getResources().getColor(R.color.green));
			holder.txt_state.setTextColor(context.getResources().getColor(R.color.green));
		}
		else
		{
			holder.img_state.setVisibility(View.GONE);
			holder.txt_ssid.setTextColor(context.getResources().getColor(R.color.black));
			holder.txt_state.setTextColor(context.getResources().getColor(R.color.black));
		}
		return convertView;
	}
	
	private String getStateText(boolean state,String type)
	{
		String text = "";
		if(state)
		{
			text = context.getString(R.string.connected);
		}
		else
		{
			if(type.indexOf("WPA-")>-1&&type.indexOf("WPA2-")>-1
					&&type.indexOf("WPS-")>-1)
			{
				text = context.getString(R.string.wpawps);
			}
			else if(type.indexOf("WPA-")>-1&&type.indexOf("WPA2-")>-1)
			{
				text = context.getString(R.string.wpawpa2);
			}
			else if(type.indexOf("WPA-")>-1)
			{
				text = context.getString(R.string.wpa);
			}
			else if(type.indexOf("WPA2-")>-1)
			{
				text = context.getString(R.string.wpa2);
			}
			else if(type.indexOf("WPS")>-1){
				
				text = context.getString(R.string.wps);
			}
		}
		
		return text;
	}
	/**
	 * 获取WIFI level对应的图片
	 * @param level
	 * @return
	 */
	private int getWifiLevelImageId(int level)
	{
		int id = 0;
		switch(level)
		{
			case 1:
				   id = R.drawable.wifi_signal1;
				   break;
			case 2:
				   id = R.drawable.wifi_signal2;
				   break;
			case 3:
				   id =R.drawable.wifi_signal3;
				   break;
			case 4:
				   id = R.drawable.wifi_signal4;
				   break;
			default:
				   id = R.drawable.wifi_signal1;
				   break;
		
		}
		return id;
	}
	private class ViewHolder{
		TextView txt_ssid;
		TextView txt_state;
		ImageView img_state;
		ImageView img_rssi;
	}
}
